//index.js
//获取应用实例
var util = require('../../utils/util.js')
var app = getApp()
Page({
  data: {
    userInfo: {},
    page: { next_page: 1, total_pages: 1},
    subjects: [],
    startX: 0,
    delBtn: 100,
  },

  //事件处理函数
  bindViewTap: function (event) {
    wx.navigateTo({
      url: '../subject/index?id=' + event.currentTarget.dataset.id + "&title=" + event.currentTarget.dataset.title
    })

  },

  onShow(e) {
    this.sendRequest("show")
  },
  onReachBottom(e) {
    this.sendRequest()
  },

  sendRequest(event) {
    var that = this
    var page = that.data.page.next_page
    if (page === null && event != "show") {
      return false
    }
    wx.request({
      url: "https://kaifan.chuanggj.com/wechat/subjects?page=" + page,
      header: {
        "Authorization": wx.getStorageSync("token")
      },
      success(res) {
        var data = res.data.subjects.map(function (val) {
          val.title = val.title.length > 12 ? val.title.substr(0, 8) + "..." : val.title
          val.start_time = util.formatIndex(new Date(val.start_time * 1000))
          return val
        })
        if (event == "show") {
          var results = data
        } else {
          var results = that.data.subjects.concat(data)
        }
        that.setData({
          subjects: results,
          page: res.data.page
        })
      }
    })
  },
  createTap(evt) {
    wx.navigateTo({
      url: 'create'
    })
  },
  touch1(e) {
    if (!e.currentTarget.dataset.status) {
      return false
    }
    if (e.touches.length == 1) {
      this.setData({
        startX: e.touches[0].clientX
      })
    }
  },
  touch2(e) {
    if (!e.currentTarget.dataset.status) {
      return false
    }
    var that = this
    if (e.touches.length == 1) {
      var moveX = e.touches[0].clientX
      var disX = that.data.startX - moveX
      var delBtn = that.data.delBtn
      var txtStyle = ''
      if (disX == 0 || disX < 0) {
        txtStyle = "left:0px"
      } else if (disX > 0) {
        txtStyle = "left:-" + disX + "px"
        if (disX >= delBtn) {
          txtStyle = "left:-" + delBtn + "px"
        }
      }
      var index = e.currentTarget.dataset.index
      var list = that.data.subjects
      list[index].txtStyle = txtStyle
      this.setData({
        subjects: list
      })
    }
  },
  touch3(e) {

    if (e.changedTouches.length == 1) {
      var endX = e.changedTouches[0].clientX
      var disX = this.data.startX - endX
      var delBtn = this.data.delBtn
      var txtStyle = disX > delBtn / 2 ? "left:-" + delBtn + "px" : "left:0px"
      var index = e.target.dataset.index
      var list = this.data.list
      list[index].txtStyle = txtStyle
      this.setData({
        subjects: list
      })
    }
  },

  delItem(e) {
    var that = this
    var sub = this.data.subjects
    var id = e.currentTarget.dataset.id
    var idx = e.currentTarget.dataset.index
    wx.request({
      url: "https://kaifan.chuanggj.com/wechat/subjects/" + id + "/delete_attend",
      method: "POST",
      header: {
        "Authorization": wx.getStorageSync("token")
      },
      success(res) {
        if (res.data.code > 200) {
          wx.showModal({
            title: res.data.msg,
          })
          sub[idx].txtStyle = "left: 0px:"
        } else {
          sub.splice(idx, 1)
        }
        that.setData({
          subjects: sub
        })
      },
      fail(err) {
        wx.showToast({
          title: "失败了",
        })
      }
    })
  }
})
